package com.github.ylt.protocol.http;

import com.github.ylt.player.Player;
import com.github.ylt.protocol.request.Request;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.SneakyThrows;

import java.io.File;
import java.io.RandomAccessFile;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

public class HttpServerWirter{

    private Request request;
    public HttpServerWirter(Request request){
        this.request = request;
    }

    @SneakyThrows
    public void run() {
        if(this.request != null){
            if(this.request.getPath().equals("description.xml")){
                URL resource = HttpServerHandler.class.getResource("/xml/description.xml");
                File file1 = new File(resource.getPath());
                byte[] bytes;
                try (RandomAccessFile file = new RandomAccessFile(file1, "r")) {
                    long length = file.length();
                    bytes = new byte[(int) length];
                    file.read(bytes, 0, (int) length);
                }
                ByteBuf content = Unpooled.copiedBuffer(bytes);
                String message = "HTTP/1.1 200 OK\r\n"+
                        "Content-Type: text/xml;charset=utf-8\r\n"+
                        "Content-Length: "+content.array().length+"\r\n"+
                        "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                        "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(), StandardCharsets.UTF_8)+"\r\n"+
                        "Date: "+LocalDateTime.now()+"\r\n"+
                        "Connection: close\r\n"+
                        "\r\n"+content.toString(StandardCharsets.UTF_8)+"\r\n";
                ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                this.request.getStream().write(buffer);
                return;
            }
            if(this.request.getPath().equals("dlna/RenderingControl.xml")){
                URL resource = HttpServerHandler.class.getResource("/xml/RenderingControl.xml");
                File file1 = new File(resource.getPath());
                byte[] bytes;
                try (RandomAccessFile file = new RandomAccessFile(file1, "r")) {
                    long length = file.length();
                    bytes = new byte[(int) length];
                    file.read(bytes, 0, (int) length);
                }
                ByteBuf content = Unpooled.copiedBuffer(bytes);
                String message = "HTTP/1.1 200 OK\r\n"+
                        "Content-Type: text/xml;charset=utf-8\r\n"+
                        "Content-Length: "+content.array().length+"\r\n"+
                        "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                        "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(), StandardCharsets.UTF_8)+"\r\n"+
                        "Date: "+LocalDateTime.now()+"\r\n"+
                        "Connection: close\r\n"+
                        "\r\n"+content.toString(StandardCharsets.UTF_8)+"\r\n";
                ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                this.request.getStream().write(buffer);
                return;
            }

            if(this.request.getPath().equals("dlna/ConnectionManager.xml")){
                URL resource = HttpServerHandler.class.getResource("/xml/ConnectionManager.xml");
                File file1 = new File(resource.getPath());
                byte[] bytes;
                try (RandomAccessFile file = new RandomAccessFile(file1, "r")) {
                    long length = file.length();
                    bytes = new byte[(int) length];
                    file.read(bytes, 0, (int) length);
                }
                ByteBuf content = Unpooled.copiedBuffer(bytes);
                String message = "HTTP/1.1 200 OK\r\n"+
                        "Content-Type: text/xml;charset=utf-8\r\n"+
                        "Content-Length: "+content.array().length+"\r\n"+
                        "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                        "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(), StandardCharsets.UTF_8)+"\r\n"+
                        "Date: "+LocalDateTime.now()+"\r\n"+
                        "Connection: close\r\n"+
                        "\r\n"+content.toString(StandardCharsets.UTF_8)+"\r\n";
                ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                this.request.getStream().write(buffer);
                return;
            }
            if(this.request.getPath().equals("dlna/AVTransport.xml")){
                URL resource = HttpServerHandler.class.getResource("/xml/AVTransport.xml");
                File file1 = new File(resource.getPath());
                byte[] bytes;
                try (RandomAccessFile file = new RandomAccessFile(file1, "r")) {
                    long length = file.length();
                    bytes = new byte[(int) length];
                    file.read(bytes, 0, (int) length);
                }
                ByteBuf content = Unpooled.copiedBuffer(bytes);
                String message = "HTTP/1.1 200 OK\r\n"+
                                "Content-Type: text/xml;charset=utf-8\r\n"+
                                "Content-Length: "+content.array().length+"\r\n"+
                                "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                                "SERVER: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(), StandardCharsets.UTF_8)+"\r\n"+
                                "\r\n"+content.toString(StandardCharsets.UTF_8)+"\r\n";
                ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                this.request.getStream().write(buffer);
                return;
            }
            if(this.request.getPath().equals("favicon.ico")){
                String message = "HTTP/1.1 200 OK\r\n"+
                        "Content-Type: text/xml;charset=utf-8\r\n"+
                        "Content-Length: 0\r\n"+
                        "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                        "SERVER: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(), StandardCharsets.UTF_8)+"\r\n"+
                        "\r\n";
                ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                this.request.getStream().write(buffer);
                return;
            }
            // NOTIFY请求
            if(this.request.method().equals("NOTIFY")){

            }
            // SUBSCRIBE请求
            if(this.request.method().equals("SUBSCRIBE")){

            }
            // UNSUBSCRIBE请求
            if(this.request.method().equals("UNSUBSCRIBE")){

            }
            if("RenderingControl/action".equals(this.request.getPath())){
                String soapaction = this.request.getHeaders().get("SOAPACTION").toString();
                String[] action = soapaction.replace("\"", "").split("#");
                if("GetVolume".equals(action[1])){
                    String body ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                                "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
                                   "<s:Body>"+
                                      "<u:GetVolumeResponse xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\">"+
                                         "<CurrentVolume>100</CurrentVolume>"+
                                      "</u:GetVolumeResponse>"+
                                   "</s:Body>"+
                                "</s:Envelope>";
                    String message = "HTTP/1.1 200 OK\r\n"+
                            "Content-Type: text/xml;charset=utf-8\r\n"+
                            "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(),StandardCharsets.UTF_8)+"\r\n"+
                            "Date: "+LocalDateTime.now()+"\r\n"+
                            "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                            "Ext: \r\n"+
                            "Content-Length: "+body.length()+"\r\n"+
                            "\r\n"+body;
                    ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                    this.request.getStream().write(buffer);
                    return;
                }
                if("GetVolumeDBRange".equals(action[1])){
                    String body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                                "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
                                   "<s:Body>"+
                                      "<u:GetVolumeDBRangeResponse xmlns:u=\"urn:schemas-upnp-org:service:RenderingControl:1\">"+
                                         "<MinValue>0</MinValue>"+
                                         "<MaxValue>5</MaxValue>"+
                                      "</u:GetVolumeDBRangeResponse>"+
                                   "</s:Body>"+
                                "</s:Envelope>";
                    String message = "HTTP/1.1 200 OK\r\n"+
                            "Content-Type: text/xml;charset=utf-8\r\n"+
                            "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(),StandardCharsets.UTF_8)+"\r\n"+
                            "Date: "+LocalDateTime.now()+"\r\n"+
                            "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                            "Ext: \r\n"+
                            "Content-Length: "+body.length()+"\r\n"+
                            "\r\n"+body;
                    ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                    this.request.getStream().write(buffer);
                    return;
                }
            }
            if("AVTransport/action".equals(this.request.getPath())){
                String soapaction = this.request.getHeaders().get("SOAPACTION").toString();
                String[] action = soapaction.replace("\"", "").split("#");
                if("Stop".equals(action[1])){
                    String body =
                                "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                                "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
                                   "<s:Body>"+
                                      "<u:StopResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"+
                                      "</u:StopResponse>"+
                                   "</s:Body>"+
                                "</s:Envelope>";
                    String message = "HTTP/1.1 200 OK\r\n"+
                            "Content-Type: text/xml;charset=utf-8\r\n"+
                            "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(),StandardCharsets.UTF_8)+"\r\n"+
                            "Date: "+LocalDateTime.now()+"\r\n"+
                            "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                            "Ext: \r\n"+
                            "Content-Length: "+body.length()+"\r\n"+
                            "\r\n"+body;
                    Player.getInstance().stop();
                    ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                    this.request.getStream().write(buffer);
                    return;
                }
                if("SetAVTransportURI".equals(action[1])){
                    String body ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                                "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
                                   "<s:Body>"+
                                      "<u:SetAVTransportURIResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"+
                                      "</u:SetAVTransportURIResponse>"+
                                   "</s:Body>"+
                                "</s:Envelope>";
                    String message = "HTTP/1.1 200 OK\r\n"+
                                "Content-Type: text/xml;charset=utf-8\r\n"+
                                "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(),StandardCharsets.UTF_8)+"\r\n"+
                                "Date: "+LocalDateTime.now()+"\r\n"+
                                "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                                "Ext: \r\n"+
                                "Content-Length: "+body.length()+"\r\n"+
                                "\r\n"+
                                body;
                    Player instance = Player.getInstance();
                    // instance.start(this.request.getParameters().get("CurrentURI"));
                    ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                    this.request.getStream().write(buffer);
                    return;
                }
                if("Play".equals(action[1])){
                    String body ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                            "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
                            "<s:Body>"+
                            "<u:PlayResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"+
                            "</u:PlayResponse>"+
                            "</s:Body>"+
                            "</s:Envelope>";
                    String message = "HTTP/1.1 200 OK\r\n"+
                            "Content-Type: text/xml;charset=utf-8\r\n"+
                            "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(),StandardCharsets.UTF_8)+"\r\n"+
                            "Date: "+LocalDateTime.now()+"\r\n"+
                            "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                            "Ext: \r\n"+
                            "Content-Length: "+body.length()+"\r\n"+
                            "\r\n"+
                            body;
                    ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                    this.request.getStream().write(buffer);
                    return;
                }
                if("GetTransportInfo".equals(action[1])){
                    String body ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                            "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
                            "<s:Body>"+
                            "<u:GetTransportInfoResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"+
                            "<CurrentTransportState>"+Player.getInstance().getPlayState().name()+"</CurrentTransportState>"+
                            "<CurrentTransportStatus>"+Player.getInstance().getTransportStatus().name()+"</CurrentTransportStatus>"+
                            "<CurrentSpeed>1</CurrentSpeed>"+
                            "</u:GetTransportInfoResponse>"+
                            "</s:Body>"+
                            "</s:Envelope>";
                    String message = "HTTP/1.1 200 OK\r\n"+
                            "Content-Type: text/xml;charset=utf-8\r\n"+
                            "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(),StandardCharsets.UTF_8)+"\r\n"+
                            "Date: "+LocalDateTime.now()+"\r\n"+
                            "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                            "Ext: \r\n"+
                            "Content-Length: "+body.length()+"\r\n"+
                            "\r\n"+
                            body;
                    ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                    this.request.getStream().write(buffer);
                    return;
                }
                if("GetPositionInfo".equals(action[1])){
                    String body ="<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                            "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
                            "<s:Body>"+
                            "<u:GetPositionInfoResponse xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">"+
                            "<Track>"+Player.getInstance().getPlayState().name()+"</Track>"+
                            "<TrackDuration>"+Player.getInstance().getTransportStatus().name()+"</TrackDuration>"+
                            "<TrackMetaData>1</TrackMetaData>"+
                            "<TrackURI>当前播放的URL</TrackURI>"+
                            "<RelTime>播放的时间</RelTime>"+
                            "<AbsTime>播放的时间</AbsTime>"+
                            "<RelCount>1</RelCount>"+
                            "<AbsCount>1</AbsCount>"+
                            "</u:GetPositionInfoResponse>"+
                            "</s:Body>"+
                            "</s:Envelope>";
                    String message = "HTTP/1.1 200 OK\r\n"+
                            "Content-Type: text/xml;charset=utf-8\r\n"+
                            "Server: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(),StandardCharsets.UTF_8)+"\r\n"+
                            "Date: "+LocalDateTime.now()+"\r\n"+
                            "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                            "Ext: \r\n"+
                            "Content-Length: "+body.length()+"\r\n"+
                            "\r\n"+
                            body;
                    ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                    this.request.getStream().write(buffer);
                }
                if("Seek".equals(action[1])){

                }

            }
            if(this.request.getPath() != null){
                String message = "HTTP/1.1 404 NOT FIND\r\n"+
                        "Content-Type: text/xml;charset=utf-8\r\n"+
                        "Content-Length: 0\r\n"+
                        "Allow: GET, HEAD, POST, SUBSCRIBE, UNSUBSCRIBE\r\n"+
                        "SERVER: "+new String("ubuntu/22.04 UPnP/1.0 我是你爹/1.0".getBytes(), StandardCharsets.UTF_8)+"\r\n"+
                        "\r\n";
                ByteBuffer buffer = ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8));
                this.request.getStream().write(buffer);
            }
        }
    }
}
